package chaoyue.study.controller;

import chaoyue.study.bean.Depart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 通过nacos进行服务调用
 */
@RestController
@RequestMapping("depart")
public class DepartController {
    @Autowired
    private RestTemplate template;

    private static final String SERVICE_PROVIDER = "http://depart-provider/depart/";

    @PostMapping("save")
    public void saveHandle(@RequestBody Depart depart) {
        template.postForObject(SERVICE_PROVIDER + "save", depart, Void.class);
    }

    @DeleteMapping("del/{id}")
    public void deleteHandle(@PathVariable("id") Integer id) {
        template.delete(SERVICE_PROVIDER + "delete/" + id);
    }

    @PostMapping("update")
    public void updateHandle(@RequestBody Depart depart) {
        template.put(SERVICE_PROVIDER + "update", depart, Void.class);
    }

    @GetMapping("get/{id}")
    public Depart getHandle(@PathVariable("id") Integer id) {
        return template.getForObject(SERVICE_PROVIDER + "get/" + id, Depart.class);
    }

    @GetMapping("list")
    public List<Depart> listHandle() {
        return template.getForObject(SERVICE_PROVIDER + "list", List.class);
    }

}
